package com.edu.controller;

import com.edu.service.UserService;
import com.edu.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

@WebServlet("/login.do")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserService us = new UserServiceImpl();
        String userName = req.getParameter("username");
        String password = req.getParameter("password");
        Map<String,Object> result = us.selectUser(userName,password);
        System.out.println(result);
        if(result.get("loginAdmin")!=null) {
            HttpSession session = req.getSession();
            session.setAttribute("loginAdmin", result.get("loginAdmin"));
            resp.sendRedirect("Goods.do");
        }else {
            req.setAttribute("msg", result.get("msg"));
            req.getRequestDispatcher("index.jsp").forward(req, resp);
        }

    }
}
